package cn.ctodb.mall.goods.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.mall.goods.service.GoodAttrService;
import cn.ctodb.mall.goods.web.rest.util.HeaderUtil;
import cn.ctodb.mall.goods.web.rest.util.PaginationUtil;
import cn.ctodb.mall.goods.service.dto.GoodAttrDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing GoodAttr.
 */
@RestController
@RequestMapping("/api")
public class GoodAttrResource {

    private final Logger log = LoggerFactory.getLogger(GoodAttrResource.class);

    private static final String ENTITY_NAME = "goodAttr";
        
    private final GoodAttrService goodAttrService;

    public GoodAttrResource(GoodAttrService goodAttrService) {
        this.goodAttrService = goodAttrService;
    }

    /**
     * POST  /good-attrs : Create a new goodAttr.
     *
     * @param goodAttrDTO the goodAttrDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new goodAttrDTO, or with status 400 (Bad Request) if the goodAttr has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/good-attrs")
    @Timed
    public ResponseEntity<GoodAttrDTO> createGoodAttr(@RequestBody GoodAttrDTO goodAttrDTO) throws URISyntaxException {
        log.debug("REST request to save GoodAttr : {}", goodAttrDTO);
        if (goodAttrDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new goodAttr cannot already have an ID")).body(null);
        }
        GoodAttrDTO result = goodAttrService.save(goodAttrDTO);
        return ResponseEntity.created(new URI("/api/good-attrs/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /good-attrs : Updates an existing goodAttr.
     *
     * @param goodAttrDTO the goodAttrDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated goodAttrDTO,
     * or with status 400 (Bad Request) if the goodAttrDTO is not valid,
     * or with status 500 (Internal Server Error) if the goodAttrDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/good-attrs")
    @Timed
    public ResponseEntity<GoodAttrDTO> updateGoodAttr(@RequestBody GoodAttrDTO goodAttrDTO) throws URISyntaxException {
        log.debug("REST request to update GoodAttr : {}", goodAttrDTO);
        if (goodAttrDTO.getId() == null) {
            return createGoodAttr(goodAttrDTO);
        }
        GoodAttrDTO result = goodAttrService.save(goodAttrDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, goodAttrDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /good-attrs : get all the goodAttrs.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of goodAttrs in body
     */
    @GetMapping("/good-attrs")
    @Timed
    public ResponseEntity<List<GoodAttrDTO>> getAllGoodAttrs(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of GoodAttrs");
        Page<GoodAttrDTO> page = goodAttrService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/good-attrs");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /good-attrs/:id : get the "id" goodAttr.
     *
     * @param id the id of the goodAttrDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the goodAttrDTO, or with status 404 (Not Found)
     */
    @GetMapping("/good-attrs/{id}")
    @Timed
    public ResponseEntity<GoodAttrDTO> getGoodAttr(@PathVariable Long id) {
        log.debug("REST request to get GoodAttr : {}", id);
        GoodAttrDTO goodAttrDTO = goodAttrService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(goodAttrDTO));
    }

    /**
     * DELETE  /good-attrs/:id : delete the "id" goodAttr.
     *
     * @param id the id of the goodAttrDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/good-attrs/{id}")
    @Timed
    public ResponseEntity<Void> deleteGoodAttr(@PathVariable Long id) {
        log.debug("REST request to delete GoodAttr : {}", id);
        goodAttrService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/good-attrs?query=:query : search for the goodAttr corresponding
     * to the query.
     *
     * @param query the query of the goodAttr search 
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/good-attrs")
    @Timed
    public ResponseEntity<List<GoodAttrDTO>> searchGoodAttrs(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of GoodAttrs for query {}", query);
        Page<GoodAttrDTO> page = goodAttrService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/good-attrs");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }


}
